br-scraper from https://github.com/andrewblim/br-scraper is a nice little baseball-reference.com scraping utility.


In [2]:
import brscraper
scraper = brscraper.BRScraper()

Now let's download all the games from April for the Cubs and Sox.


In [3]:
from datetime import datetime
teams = {'CHC': [], 'CHW': []}
for team in teams:
    for year in range(1901, 2016):
        data = scraper.parse_tables("teams/%s/%d-schedule-scores.shtml" % (team, year))
        last_date = None
        for game in data['team_schedule']:
            if game['Date']:
                date = datetime.strptime(game['Date'], '%A, %b %d')
                game['parsed_date'] = date
                game['year'] = year
                last_date = date
                if date.month < 5:
                    teams[team].append(game)
                else:
                    break
            else:
                game['parsed_date'] = None
                game['year'] = year
                print year
                teams[team].append(game)


1907
1907
1909
1909
1909
1909
1933
1933
1935
1935
1944
1944
1944
1944
1945
1945
1945
1945
1946
1946
1954
1954
1955
1955
1955
1955
1956
1956
1956
1956
1957
1957
1957
1957
1961
1961
1961
1961
1962
1962
1962
1962
1963
1963
1965
1965
1968
1968
1969
1969
1969
1969
1969
1969
1971
1971
1972
1972
1973
1973
1973
1973
1974
1974
1975
1975
1981
1981
1984
1984
1990
1990
1997
1997
2000
2000
2001
2001
2002
2002
2005
2005
2011
2011
2014
2014
1944
1944
1944
1944
1945
1945
1945
1945
1946
1946
1946
1946
1947
1947
1948
1948
1949
1949
1949
1949
1949
1949
1950
1950
1951
1951
1952
1952
1952
1952
1952
1952
1953
1953
1954
1954
1955
1955
1955
1955
1959
1959
1960
1960
1961
1961
1962
1962
1962
1962
1963
1963
1963
1963
1963
1963
1965
1965
1967
1967
1967
1967
1968
1968
1969
1969
1970
1970
1971
1971
1971
1971
1972
1972
1972
1972
1973
1973
1975
1975
1977
1977
1981
1981
1982
1982
1982
1982
1982
1982
1997
1997
1997
1997
1999
1999
2004
2004
2008
2008
2009
2009
/usr/local/lib/python2.7/site-packages/bs4/element.py:1413: UserWarning: has_key is deprecated. Use has_attr("class") instead.
  key))

In [4]:
print 'Cubs have %d games, Sox have %d' % (len(teams['CHC']), len(teams['CHW']))


Cubs have 1871 games, Sox have 1800

In [5]:
def create_totals():
    return {'CHC': {'wins': 0, 'losses': 0, 'ties': 0}, 'CHW': {'wins': 0, 'losses': 0, 'ties': 0}}

for year in range(1901, 2016):
    totals = create_totals()
    for team in totals.keys():
        missing_games = False
        for game in teams[team]:
            if not game['parsed_date'] and game['year'] == year:
                missing_games = True
            elif game['year'] == year:
                if game['W/L'].lower().startswith('w'):
                    totals[team]['wins'] += 1
                elif game['W/L'].lower().startswith('l'):
                    totals[team]['losses'] += 1
                elif game['W/L'].lower().startswith('t'):
                    totals[team]['ties'] += 1
                else:
                    print 'wtf: %s' % game['W/L']
        denominator = float(totals[team]['wins'] + totals[team]['losses'] + totals[team]['ties'])
        if denominator != 0:
            print '%s (%d): %d-%d-%d %s(.%.3f)' % (
                team,
                year,
                totals[team]['wins'],
                totals[team]['losses'],
                totals[team]['ties'],
                '' if not missing_games else '(missing some) ',
                float(totals[team]['wins'])/denominator
            )
    print '-----'


CHC (1901): 3-6-0 (.0.333)
CHW (1901): 4-2-0 (.0.667)
-----
CHC (1902): 6-3-0 (.0.667)
CHW (1902): 4-2-0 (.0.667)
-----
CHC (1903): 7-4-0 (.0.636)
CHW (1903): 3-3-0 (.0.500)
-----
CHC (1904): 5-5-1 (.0.455)
CHW (1904): 7-6-1 (.0.500)
-----
CHC (1905): 7-6-0 (.0.538)
CHW (1905): 7-5-0 (.0.583)
-----
CHC (1906): 10-6-0 (.0.625)
CHW (1906): 7-6-0 (.0.538)
-----
CHC (1907): 11-2-0 (missing some) (.0.846)
CHW (1907): 9-4-0 (.0.692)
-----
CHC (1908): 8-3-0 (.0.727)
CHW (1908): 7-7-0 (.0.500)
-----
CHC (1909): 7-4-0 (missing some) (.0.636)
CHW (1909): 6-5-0 (.0.545)
-----
CHC (1910): 6-4-0 (.0.600)
CHW (1910): 4-5-0 (.0.444)
-----
CHC (1911): 9-6-2 (.0.529)
CHW (1911): 7-7-0 (.0.500)
-----
CHC (1912): 5-7-0 (.0.417)
CHW (1912): 10-4-1 (.0.667)
-----
CHC (1913): 12-4-1 (.0.706)
CHW (1913): 10-8-0 (.0.556)
-----
CHC (1914): 5-8-1 (.0.357)
CHW (1914): 7-7-0 (.0.500)
-----
CHC (1915): 8-6-1 (.0.533)
CHW (1915): 10-7-0 (.0.588)
-----
CHC (1916): 8-5-0 (.0.615)
CHW (1916): 9-9-0 (.0.500)
-----
CHC (1917): 9-7-0 (.0.562)
CHW (1917): 10-6-0 (.0.625)
-----
CHC (1918): 6-3-0 (.0.667)
CHW (1918): 5-2-0 (.0.714)
-----
CHC (1919): 3-3-0 (.0.500)
CHW (1919): 6-1-0 (.0.857)
-----
CHC (1920): 4-8-0 (.0.333)
CHW (1920): 7-2-0 (.0.778)
-----
CHC (1921): 6-5-0 (.0.545)
CHW (1921): 4-6-0 (.0.400)
-----
CHC (1922): 11-5-0 (.0.688)
CHW (1922): 8-6-1 (.0.533)
-----
CHC (1923): 7-6-0 (.0.538)
CHW (1923): 2-9-1 (.0.167)
-----
CHC (1924): 9-6-0 (.0.600)
CHW (1924): 7-6-0 (.0.538)
-----
CHC (1925): 8-6-0 (.0.571)
CHW (1925): 9-6-0 (.0.600)
-----
CHC (1926): 8-7-1 (.0.500)
CHW (1926): 11-6-0 (.0.647)
-----
CHC (1927): 7-7-0 (.0.500)
CHW (1927): 9-7-0 (.0.562)
-----
CHC (1928): 9-10-0 (.0.474)
CHW (1928): 7-10-1 (.0.389)
-----
CHC (1929): 7-5-0 (.0.583)
CHW (1929): 6-6-0 (.0.500)
-----
CHC (1930): 8-8-0 (.0.500)
CHW (1930): 6-4-0 (.0.600)
-----
CHC (1931): 8-3-0 (.0.727)
CHW (1931): 6-6-0 (.0.500)
-----
CHC (1932): 11-3-0 (.0.786)
CHW (1932): 5-11-0 (.0.312)
-----
CHC (1933): 5-7-0 (missing some) (.0.417)
CHW (1933): 10-6-0 (.0.625)
-----
CHC (1934): 10-2-0 (.0.833)
CHW (1934): 4-6-0 (.0.400)
-----
CHC (1935): 8-5-0 (missing some) (.0.615)
CHW (1935): 9-3-0 (.0.750)
-----
CHC (1936): 8-6-0 (.0.571)
CHW (1936): 4-8-0 (.0.333)
-----
CHC (1937): 2-6-0 (.0.250)
CHW (1937): 3-3-0 (.0.500)
-----
CHC (1938): 8-4-0 (.0.667)
CHW (1938): 5-5-0 (.0.500)
-----
CHC (1939): 6-4-0 (.0.600)
CHW (1939): 6-4-0 (.0.600)
-----
CHC (1940): 7-7-0 (.0.500)
CHW (1940): 3-7-0 (.0.300)
-----
CHC (1941): 5-7-0 (.0.417)
CHW (1941): 9-4-0 (.0.692)
-----
CHC (1942): 7-9-0 (.0.438)
CHW (1942): 3-12-0 (.0.200)
-----
CHC (1943): 3-4-0 (.0.429)
CHW (1943): 1-4-0 (.0.200)
-----
CHC (1944): 1-5-0 (missing some) (.0.167)
CHW (1944): 2-3-0 (missing some) (.0.400)
-----
CHC (1945): 5-2-0 (missing some) (.0.714)
CHW (1945): 3-0-0 (missing some) (.1.000)
-----
CHC (1946): 5-5-0 (missing some) (.0.500)
CHW (1946): 4-5-0 (missing some) (.0.444)
-----
CHC (1947): 8-5-0 (.0.615)
CHW (1947): 6-2-0 (missing some) (.0.750)
-----
CHC (1948): 4-6-0 (.0.400)
CHW (1948): 2-5-0 (missing some) (.0.286)
-----
CHC (1949): 5-6-0 (.0.455)
CHW (1949): 4-3-0 (missing some) (.0.571)
-----
CHC (1950): 3-2-0 (.0.600)
CHW (1950): 1-4-0 (missing some) (.0.200)
-----
CHC (1951): 5-5-0 (.0.500)
CHW (1951): 5-3-0 (missing some) (.0.625)
-----
CHC (1952): 9-4-0 (.0.692)
CHW (1952): 3-5-0 (missing some) (.0.375)
-----
CHC (1953): 5-4-0 (.0.556)
CHW (1953): 7-5-0 (missing some) (.0.583)
-----
CHC (1954): 3-5-0 (missing some) (.0.375)
CHW (1954): 7-5-0 (missing some) (.0.583)
-----
CHC (1955): 6-7-0 (missing some) (.0.462)
CHW (1955): 8-4-0 (missing some) (.0.667)
-----
CHC (1956): 2-5-0 (missing some) (.0.286)
CHW (1956): 5-1-0 (.0.833)
-----
CHC (1957): 2-5-0 (missing some) (.0.286)
CHW (1957): 8-2-0 (.0.800)
-----
CHC (1958): 8-5-0 (.0.615)
CHW (1958): 3-9-0 (.0.250)
-----
CHC (1959): 8-8-0 (.0.500)
wtf: 
CHW (1959): 7-6-0 (missing some) (.0.538)
-----
CHC (1960): 3-10-0 (.0.231)
CHW (1960): 5-4-0 (missing some) (.0.556)
-----
CHC (1961): 7-5-0 (missing some) (.0.583)
CHW (1961): 6-5-0 (missing some) (.0.545)
-----
CHC (1962): 2-14-0 (missing some) (.0.125)
CHW (1962): 9-5-0 (missing some) (.0.643)
-----
CHC (1963): 8-9-0 (missing some) (.0.471)
CHW (1963): 6-4-0 (missing some) (.0.600)
-----
CHC (1964): 4-7-0 (.0.364)
CHW (1964): 5-4-0 (.0.556)
-----
CHC (1965): 7-6-1 (missing some) (.0.500)
CHW (1965): 7-3-0 (missing some) (.0.700)
-----
CHC (1966): 3-12-0 (.0.200)
CHW (1966): 10-3-0 (.0.769)
-----
CHC (1967): 8-6-0 (.0.571)
CHW (1967): 6-6-0 (missing some) (.0.500)
-----
CHC (1968): 6-10-0 (missing some) (.0.375)
CHW (1968): 2-10-0 (missing some) (.0.167)
-----
CHC (1969): 14-3-0 (missing some) (.0.824)
CHW (1969): 6-8-0 (missing some) (.0.429)
-----
CHC (1970): 13-5-0 (.0.722)
CHW (1970): 7-10-0 (missing some) (.0.412)
-----
CHC (1971): 8-11-0 (missing some) (.0.421)
CHW (1971): 6-11-0 (missing some) (.0.353)
-----
CHC (1972): 4-8-0 (missing some) (.0.333)
CHW (1972): 6-3-0 (missing some) (.0.667)
-----
CHC (1973): 10-6-0 (missing some) (.0.625)
CHW (1973): 9-4-0 (missing some) (.0.692)
-----
CHC (1974): 6-10-0 (missing some) (.0.375)
CHW (1974): 8-11-2 (.0.381)
-----
CHC (1975): 12-3-0 (missing some) (.0.800)
CHW (1975): 5-13-0 (missing some) (.0.278)
-----
CHC (1976): 9-10-0 (.0.474)
wtf: 
CHW (1976): 5-6-0 (.0.455)
-----
CHC (1977): 7-9-0 (.0.438)
CHW (1977): 9-7-0 (missing some) (.0.562)
-----
CHC (1978): 11-9-0 (.0.550)
CHW (1978): 6-12-0 (.0.333)
-----
wtf: 
CHC (1979): 8-8-0 (.0.500)
CHW (1979): 9-11-0 (.0.450)
-----
CHC (1980): 9-6-0 (.0.600)
CHW (1980): 12-6-0 (.0.667)
-----
CHC (1981): 1-13-0 (missing some) (.0.071)
CHW (1981): 9-6-0 (missing some) (.0.600)
-----
CHC (1982): 7-14-0 (.0.333)
CHW (1982): 7-6-0 (missing some) (.0.538)
-----
CHC (1983): 6-14-0 (.0.300)
CHW (1983): 8-10-0 (.0.444)
-----
CHC (1984): 11-7-0 (missing some) (.0.611)
CHW (1984): 8-13-0 (.0.381)
-----
CHC (1985): 12-6-0 (.0.667)
CHW (1985): 9-8-0 (.0.529)
-----
CHC (1986): 7-12-0 (.0.368)
CHW (1986): 7-12-0 (.0.368)
-----
CHC (1987): 10-10-0 (.0.500)
CHW (1987): 6-12-0 (.0.333)
-----
wtf: 
CHC (1988): 10-11-0 (.0.476)
CHW (1988): 11-10-0 (.0.524)
-----
CHC (1989): 12-11-0 (.0.522)
CHW (1989): 8-16-0 (.0.333)
-----
CHC (1990): 7-10-0 (missing some) (.0.412)
CHW (1990): 10-6-0 (.0.625)
-----
CHC (1991): 10-11-0 (.0.476)
CHW (1991): 11-6-0 (.0.647)
-----
CHC (1992): 7-13-0 (.0.350)
CHW (1992): 11-8-0 (.0.579)
-----
CHC (1993): 11-11-0 (.0.500)
CHW (1993): 13-9-0 (.0.591)
-----
CHC (1994): 6-15-0 (.0.286)
CHW (1994): 13-10-0 (.0.565)
-----
CHC (1995): 4-1-0 (.0.800)
CHW (1995): 1-4-0 (.0.200)
-----
CHC (1996): 13-14-0 (.0.481)
CHW (1996): 15-10-0 (.0.600)
-----
CHC (1997): 5-18-0 (missing some) (.0.217)
CHW (1997): 7-16-0 (missing some) (.0.304)
-----
CHC (1998): 14-13-0 (.0.519)
CHW (1998): 10-15-1 (.0.385)
-----
CHC (1999): 10-10-0 (.0.500)
CHW (1999): 9-9-0 (missing some) (.0.500)
-----
CHC (2000): 10-15-0 (missing some) (.0.400)
CHW (2000): 17-8-0 (.0.680)
-----
CHC (2001): 13-9-0 (missing some) (.0.591)
CHW (2001): 8-15-0 (.0.348)
-----
CHC (2002): 8-14-0 (missing some) (.0.364)
CHW (2002): 16-10-0 (.0.615)
-----
CHC (2003): 15-12-0 (.0.556)
CHW (2003): 14-13-0 (.0.519)
-----
CHC (2004): 13-9-0 (.0.591)
CHW (2004): 13-8-0 (missing some) (.0.619)
-----
CHC (2005): 11-10-0 (missing some) (.0.524)
CHW (2005): 17-7-0 (.0.708)
-----
CHC (2006): 13-10-0 (.0.565)
CHW (2006): 17-7-0 (.0.708)
-----
CHC (2007): 10-14-0 (.0.417)
CHW (2007): 12-11-0 (.0.522)
-----
CHC (2008): 17-10-0 (.0.630)
CHW (2008): 13-12-0 (missing some) (.0.520)
-----
CHC (2009): 10-11-0 (.0.476)
CHW (2009): 10-9-0 (missing some) (.0.526)
-----
CHC (2010): 11-13-0 (.0.458)
CHW (2010): 9-14-0 (.0.391)
-----
CHC (2011): 11-13-0 (missing some) (.0.458)
CHW (2011): 10-18-0 (.0.357)
-----
CHC (2012): 8-15-0 (.0.348)
CHW (2012): 11-11-0 (.0.500)
-----
CHC (2013): 10-16-0 (.0.385)
CHW (2013): 10-15-0 (.0.400)
-----
CHC (2014): 9-15-0 (missing some) (.0.375)
CHW (2014): 14-15-0 (.0.483)
-----
CHC (2015): 12-8-0 (.0.600)
CHW (2015): 8-11-0 (.0.421)
-----

In [ ]: